﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using DotLiquid;
using Quizz.Infrastructure.Common;
using Quizz.Library.QuizzServiceReference;

namespace Quizz.Library.Helper
{
    public sealed class MailHelper
    {
        public static void SendRegisterEmail(User user)
        {
            var templateFile = ConfigurationManager.AppSettings["MailRegisterTemplate"];
            var path = AccessFactory.MapServerPath(templateFile);
            if(!File.Exists(path))
                throw new FileNotFoundException("Mail template file for new account not found !\nPlease check your configuration.");

            var file = new StreamReader(path, Encoding.UTF8);

            var template = Template.Parse(file.ReadToEnd());
            file.Close();
            var result =
                template.Render(
                    Hash.FromAnonymousObject(
                        new
                            {
                                firstname = user.FirstName,
                                lastname = user.LastName,
                                email = user.Email,
                                password = "*************"
                            }));

            MailSender.Send(new string[] {user.Email}, "Quizz.com : Thông tin tài khoản", result, true);
        }

        public static void SendNewPassEmail(User user, string newPass)
        {
            var templateFile = ConfigurationManager.AppSettings["NewPasswordTemplate"];
            var path = AccessFactory.MapServerPath(templateFile);
            if(!File.Exists(path))
                throw new FileNotFoundException("Mail template file for new password request not found !\nPlease check your configuration.");
            var file = new StreamReader(path, Encoding.UTF8);

            var template = Template.Parse(file.ReadToEnd());
            file.Close();

            var result =
                template.Render(
                    Hash.FromAnonymousObject(
                        new
                            {
                                firstname = user.FirstName,
                                lastname = user.LastName,
                                email = user.Email,
                                password = newPass
                            }));

            MailSender.Send(new string[] {user.Email}, "Quizz.com : Mật khẩu mới", result, true);
        }

        public static void SendChangeProfile(User user)
        {
            var templateFile = ConfigurationManager.AppSettings["ChangeProfileTemplate"];
            var path = AccessFactory.MapServerPath(templateFile);
            if (!File.Exists(path))
                throw new FileNotFoundException("Mail template file for change profile not found !\nPlease check your configuration.");
            var file = new StreamReader(path, Encoding.UTF8);
            var template = Template.Parse(file.ReadToEnd());
            file.Close();
            var result = template.Render(Hash.FromAnonymousObject(
                new {
                    firstname= user.FirstName,
                    lastname= user.LastName,
                    email= user.Email,
                    password= "*************",
                    phone= user.Phone,
                    address= user.Address
                }
                ));
            MailSender.Send(new string[] { user.Email }, "Quizz.com : Thay đổi thông tin", result, true);
        }
    }
}
